/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sarco.bwi.lib;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author iktsup
 */
public class Config {
    private String[] configF;
    
    public String[] nodes;
    public String blenderPath;
    public String blend;
    public String format;
    public String ouput;
    public String master;
    public int port;
    public int startFrame;
    public int endFrame;

    private static String SPLIT = ";";

    public static String COMMENT        = "#";
    public static String NODE           = "node=";
    public static String BLENDER_PATH   = "blender=";
    public static String BLEND_FILE     = "blend=";
    public static String FORMAT         = "format=";
    public static String OUTPUT         = "output=";
    public static String MASTER         = "master=";
    public static String PORT           = "port=";
    public static String START_FRAME    = "start=";
    public static String END_FRAME      = "end=";

    public Config() {
        
    }

    public void loadConfig(String config) throws FileNotFoundException,
            IOException {
        BufferedReader in = new BufferedReader(new FileReader(config));
        String conf = "";

        String s = in.readLine();
        while (s != null) {
            conf += s + Config.SPLIT;
            s = in.readLine();
        }

        // <START> Clean up config :
        conf = conf.replaceAll("\"", "");
        // <END>

        this.configF = conf.split(Config.SPLIT);

        this.readConfig();
    }

    private void readConfig() {
        String node = "";

        for (int i = 0; i < this.configF.length; i++) {
            String line = this.configF[i];
            if (line.startsWith(Config.COMMENT)) { /* -- SKIP -- */ }
            else if(line.startsWith(Config.NODE)) {
                String s = line.substring(Config.NODE.length());
                node += s + Config.SPLIT;
            } else if (line.startsWith(Config.BLENDER_PATH)) {
                String s = line.substring(Config.BLENDER_PATH.length());
                this.blenderPath = s;
            } else if (line.startsWith(Config.BLEND_FILE)) {
                String s = line.substring(Config.BLEND_FILE.length());
                this.blend = s;
            } else if (line.startsWith(Config.FORMAT)) {
                String s = line.substring(Config.FORMAT.length());
                this.format = s;
            } else if (line.startsWith(Config.OUTPUT)) {
                String s = line.substring(Config.OUTPUT.length());
                this.ouput = s;
            } else if (line.startsWith(Config.MASTER)) {
                String s = line.substring(Config.MASTER.length());
                this.master = s;
            } else if (line.startsWith(Config.PORT)) {
                String s = line.substring(Config.PORT.length());
                this.port = Integer.parseInt(s);
            } else if (line.startsWith(Config.START_FRAME)) {
                String s = line.substring(Config.START_FRAME.length());
                this.startFrame = Integer.parseInt(s);
            } else if (line.startsWith(Config.END_FRAME)) {
                String s = line.substring(Config.END_FRAME.length());
                this.endFrame = Integer.parseInt(s);
            }
        }

        this.nodes = node.split(Config.SPLIT);
    }
}
